package com.tcxr.web;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.tcxr.framework.base.WebBase;
import com.tcxr.framework.system.RandomSys;
import com.tcxr.vo.LoginView;
import com.tcxr.vo.MyFormSelect;
import com.tcxr.vo.MyTagView;

@Controller
public class SystemWeb extends WebBase {
	public static String FORM_VERIFICATION_CODE="FORM_VER_COD";
	
//		@RequestMapping("/system/jpgVerify")
//	    public void handleRequest(ModelMap modelMap,HttpServletRequest request,HttpServletResponse response) throws IOException  {  
//			
//
//			  //禁止缓存
//			  response.setHeader("Pragma", "No-cache");
//			  response.setHeader("Cache-Control", "No-cache");
//			  response.setDateHeader("Expires", 0);
//			  // 指定生成的响应是图片
//			  response.setContentType("image/jpeg");
//			  
//			  
//			  
//			  
//			  int width = 200;
//			  int height = 60;
//			  BufferedImage image = new BufferedImage(width, height,
//			    BufferedImage.TYPE_INT_RGB);    //创建BufferedImage类的对象
//			  Graphics g = image.getGraphics();     //创建Graphics类的对象
//			  Graphics2D g2d=(Graphics2D)g;      //通过Graphics类的对象创建一个Graphics2D类的对象
//			  Random random = new Random();      //实例化一个Random对象
//			  Font mFont = new Font("华文宋体", Font.BOLD, 30);  //通过Font构造字体
//			  g.setColor(getRandColor(200, 250));     //改变图形的当前颜色为随机生成的颜色
//			  g.fillRect(0, 0, width , height);     //绘制一个填色矩形
//
//			  //画一条折线  
//			  BasicStroke bs=new BasicStroke(2f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL); //创建一个供画笔选择线条粗细的对象
//			  g2d.setStroke(bs);     //改变线条的粗细
//			  g.setColor(Color.DARK_GRAY);  //设置当前颜色为预定义颜色中的深灰色
//			  int[] xPoints=new int[3];
//			  int[] yPoints=new int[3];
//			  for(int j=0;j<3;j++){
//			   xPoints[j]=random.nextInt(width - 1);
//			   yPoints[j]=random.nextInt(height - 1);
//			  }
//			  g.drawPolyline(xPoints, yPoints,3);
//			  //生成并输出随机的验证文字  
//			  g.setFont(mFont);  
//			  String sRand="";
//			  int itmp=0;
//			  for(int i=0;i<4;i++){
//			   if(random.nextInt(2)==1){
//			    itmp=random.nextInt(26)+65; //生成A~Z的字母
//			   }else{
//			    itmp=random.nextInt(10)+48; //生成0~9的数字
//			   }
//			   char ctmp=(char)itmp;
//			   sRand+=String.valueOf(ctmp);
//			   Color color=new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110));
//			   g.setColor(color);
//			   /****随机缩放文字并将文字旋转指定角度**/
//			   //将文字旋转指定角度
//			   Graphics2D g2d_word=(Graphics2D)g;
//			   AffineTransform trans=new AffineTransform();
//			   trans.rotate(random.nextInt(45)*3.14/180,15*i+10,7);
//			   //缩放文字
//			   float scaleSize=random.nextFloat()+0.8f;
//			   if(scaleSize>1.1f) scaleSize=1f;
//			   trans.scale(scaleSize, scaleSize);
//			   g2d_word.setTransform(trans);
//			   /************************/
//			   g.drawString(String.valueOf(ctmp),30*i+40,16);
//
//			  }
//			  //将生成的验证码保存到Session中
//			  HttpSession session=request.getSession(true);
//			  session.setAttribute("randCheckCode",sRand);
//			  g.dispose();
//			  ImageIO.write(image,"JPEG",response.getOutputStream());
//			 }
		  public Color getRandColor(int s, int e) {
			  Random random = new Random();
			  if (s > 255) s = 255;
			  if (e > 255) e = 255;
			  int r = s + random.nextInt(e - s);
			  int g = s + random.nextInt(e - s);
			  int b = s + random.nextInt(e - s);
			  return new Color(r, g, b);
			 }
		
		
			@RequestMapping("/system/jpgVerify")
		    public void handleRequest2(ModelMap modelMap,HttpServletRequest request,HttpServletResponse response) throws IOException  {  
				
				
	 
		  try {  
			   int width = 50;  
			   int height = 18;  
			   // 取得一个4位随机字母数字字符串  
			   String s = RandomSys.getRandomNumber();//这个s的值就是页面验证码上显示的值  
			   
			   super.getSession().setAttribute(FORM_VERIFICATION_CODE, s);
			     
			   // 保存入session,用于与用户的输入进行比较.  
			   // 注意比较完之后清除session.  
//			   HttpSession session = request.getSession(true);  
//			   session.setAttribute("validateCode", s);  
			  
			
			   response.setHeader("Pragma","No-cache");  
			   response.setHeader("Cache-Control", "no-cache");  
			   response.setDateHeader("Expires", 0);  
			   response.setContentType("image/jpeg");  
			  
			  
			   
			     
			  // ServletOutputStream out = response.getOutputStream();  
			   BufferedImage image = new BufferedImage(width, height,  
			     BufferedImage.TYPE_INT_RGB);  
			   Graphics g = image.getGraphics();  
			   // 设定背景色  
			   g.setColor(getRandColor(200, 250));  
			   g.fillRect(0, 0, width, height);  
			  
			   // 设定字体  
			   Font mFont = new Font("Times New Roman", Font.BOLD, 18);// 设置字体  
			   g.setFont(mFont);  
			  
			   // 画边框  
			   // g.setColor(Color.BLACK);  
			   // g.drawRect(0, 0, width - 1, height - 1);  
			  
			   // 随机产生干扰线，使图象中的认证码不易被其它程序探测到  
			   g.setColor(getRandColor(160, 200));  
			   // 生成随机类  
			   Random random = new Random();  
			   for (int i = 0; i < 155; i++) {  
			    int x2 = random.nextInt(width);  
			    int y2 = random.nextInt(height);  
			    int x3 = random.nextInt(12);  
			    int y3 = random.nextInt(12);  
			    g.drawLine(x2, y2, x2 + x3, y2 + y3);  
			   }  
			  
			   // 将认证码显示到图象中  
			   g.setColor(new Color(20 + random.nextInt(110), 20 + random  
			     .nextInt(110), 20 + random.nextInt(110)));  
			  
			   g.drawString(s, 2, 16);  
			  
			   // 图象生效  
			   g.dispose();  
			   // 输出图象到页面  
			   ImageIO.write( image, "JPEG",  response.getOutputStream());  
			   // out.close();  
			  } catch (Exception e) {  
			   e.printStackTrace();  
			  }  
			  
			 }  
			  
	 /****************************************************************/		
			@RequestMapping("/formMytag")
			public String formBiaoqian(	ModelMap modelMap,
					@ModelAttribute MyTagView myTagView  )
			{
				System.out.println("复选数量"+myTagView.getSelectValueCheck().length);
				 
				getbiaoqian(	  modelMap);
				
				
				return "mytag";
			}
			
			@RequestMapping("/mytag")
			public String getbiaoqian(	ModelMap modelMap)
			{
				MyTagView myTagView=new MyTagView();
				myTagView.setName("111");
				myTagView.setSelectValue("5");
				myTagView.setSelectValueCheck(new String[]{"1","3","5","6"});
				myTagView.setSelectCheckboxs(new String[]{"1","2","5","6"});
				myTagView.setSelectRadiobutton("5");
				
				modelMap.addAttribute("myTagView", myTagView);
				
				/**************/
				//填充一个select 标签的变量 
				 List<MyFormSelect> list=new ArrayList<MyFormSelect>();
				 MyFormSelect formSelect=null;
				 for (int i = 0; i < 10; i++) {
					   formSelect=new MyFormSelect();
					 formSelect.setKey(i+"-key");
					 formSelect.setValue(i+"");
					 list.add(formSelect);
					 
				}
				modelMap.addAttribute("myFormSelect", list);
				//填充一个select 标签的变量 
				/**************/
				
				
				
				
				
				
				 return "mytag";
			}
			 
			
			
			
	 
		
		
	    } 
	 

 